package dwl;

import static org.junit.Assert.assertTrue;
import robocode.control.events.RoundStartedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;
import robocode.control.testing.RobotTestBed;

/**
 * Illustrates JUnit testing of Robocode robots. This test simply verifies that the robot is
 * constantly moving to dodge bullets.
 * 
 * @author David Lin
 */
public class TestMovement extends RobotTestBed {

  int counter = 0; // Counter for the number of turns
  double xPos = 0; // Last x position of robot
  double yPos = 0; // Last y position of robot

  /**
   * Specifies that RobotFighter and Crazy are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Crazy,dwl.RobotFighter";
  }

  /**
   * This test runs for 1 round.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 1;
  }

  /**
   * Record initial position of robot.
   * 
   * @param event Details about the started battle.
   */
  @Override
  public void onRoundStarted(RoundStartedEvent event) {
    IRobotSnapshot robot = event.getStartSnapshot().getRobots()[1];
    xPos = robot.getX();
    yPos = robot.getY();
  }

  /**
   * After 15 turns, check that robot has moved from its previous position.
   * 
   * @param event Info about the current state of the battle.
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    if (counter < 15) {
      counter++;
    }
    else {
      counter = 0;
      IRobotSnapshot robot = event.getTurnSnapshot().getRobots()[1];
      boolean moved = false;
      if (robot.getX() != xPos || robot.getY() != yPos) {
        moved = true;
      }
      assertTrue(moved);
    }
  }

}
